package com.lishuang.deviceCategory.Impl;

import com.lishuang.base.Enums.DeviceStatus;
import com.lishuang.deviceCategory.DeviceOperation;
import com.lishuang.domain.json.Device;
import com.lishuang.manager.DeviceScheduler;

/**
 * @author LiShuang
 * @version 1.0.0
 * @date 2022/11/12 16:10
 */
public class Sensor extends Device implements Runnable, DeviceOperation {

    boolean allowrunning = true;
    private Thread nowThread;

    @Override
    public void setInterrupted() {
        if(getDeviceStatus()==DeviceStatus.offline)
            return;
        super.setDeviceStatus(DeviceStatus.offline);
        setAllowrunning(false);
        nowThread.interrupt();
    }

    @Override
    public void setAllowrunning(boolean allowrunning) {
        this.allowrunning=allowrunning;
    }

    @Override
    public boolean isAllowrunning() {
        return allowrunning;
    }


    public Sensor(Device j) {
        super(j);
    }

    public Sensor(int deviceId, String deviceCategory) {
        super(deviceId, deviceCategory);
    }

    @Override
    public void run() {
        nowThread=Thread.currentThread();
        System.out.println("Sensor "+" is being served, and task series is "+super.getUuid());
        while(true){
            //TODO 加入自定义逻辑
            try {
                Thread.sleep(1000);
                //加入判断标志
                if(!allowrunning)
                    break;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
